﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Keepo.Models;
using System.IO;
using Keepo.Helpers;
using System.Text;

namespace AlbumDesigner.Helpers
{
    public class AlbumHelper
    {
        static public void CreateAlbumDirectories(int AlbumId)
        {
            var dirs = new string[] { "pages", "layouts" };

            foreach (var dir in dirs)
            {
                string dirname = "resources/albums/" + AlbumId + "/" + dir;
                dirname = HttpContext.Current.Server.MapPath("~/" + dirname);

                if (!Directory.Exists(dirname))
                {
                    Directory.CreateDirectory(dirname);
                }
            }
        }

        // Album pages
        static public string PageFullImage(int AlbumId, int id)
        {
            return "resources/albums/" + AlbumId + "/pages/" + id + "-full.jpg";
        }

        static public string PageThumbnailUrl(int AlbumId, int id)
        {
            return "resources/albums/" + AlbumId + "/pages/" + id + "-thumb.jpg";
        }

        static public string PageXmlUrl(int albumId, int pageId)
        {
            return "resources/albums/" + albumId + "/pages/" + pageId + ".xml";
        }

        // Layouts
        static public string PageLayoutXml(int? AlbumId, string guid)
        {
            if (AlbumId != null && AlbumId != -1)
            {
                return "resources/albums/" + AlbumId + "/layouts/" + guid + ".xml";
            }
            return "resources/layouts/" + guid + ".xml";
        }

        static public string PageLayoutThumbnailUrl(int? AlbumId, string guid)
        {
            if (AlbumId != null && AlbumId != -1)
            {
                return "resources/albums/" + AlbumId + "/layouts/" + guid + "-thumb.jpg";
            }
            return "resources/layouts/" + guid + "-thumb.jpg";
        }

        // Premade
        static public string PremadePageThumbnailUrl(int id)
        {
            return "resources/premade/" + id + "-thumb.jpg";
        }

        static public string PremadePageXml(int id)
        {
            return "resources/premade/" + id + ".xml";
        }

        static public long GetAlbumImageFilesSize(AlbumImage image)
        {
            long filesSize = 0;

            //calc the size of the files
            var fileName = HttpContext.Current.Server.MapPath("~/" + image.URL);
            if (File.Exists(fileName))
            {
                FileInfo file = new FileInfo(fileName);
                filesSize += file.Length;
            }
            fileName = HttpContext.Current.Server.MapPath("~/" + image.ThumbnailUrl);
            if (File.Exists(fileName))
            {
                FileInfo file = new FileInfo(fileName);
                filesSize += file.Length;
            }
            fileName = HttpContext.Current.Server.MapPath("~/" + image.LowResUrl);
            if (File.Exists(fileName))
            {
                FileInfo file = new FileInfo(fileName);
                filesSize += file.Length;
            }

            return filesSize;
        }

        static public void DeleteAlbumImageFiles(AlbumImage image)
        {
            //delete all files of this AlbumImage
            var fileName = HttpContext.Current.Server.MapPath("~/" + image.URL);
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            fileName = HttpContext.Current.Server.MapPath("~/" + image.ThumbnailUrl);
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            fileName = HttpContext.Current.Server.MapPath("~/" + image.LowResUrl);
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
        }

        static public float CalculateSpineWidthMm(Album album, Paper paper)
        {
            int numPages = AlbumPagesHelper.NumOfPagesInAlbum(album.AlbumId);
            int numSheets = numPages / 2;
            float paperThicknessMm = paper.ThicknessMM;

            float spineWidthMm = (numSheets * paperThicknessMm);

            var carton = (2 * album.AlbumSize.CartonThicknessMM);

            return spineWidthMm + carton;
        }

        static public string EditorsPasswordsTable(Album album)
        {
            var sb = new StringBuilder();

            sb.Append("<table border='1'>");
            sb.Append("<tr><th>שם מקטע</th><th>עורך</th><th>סיסמה</th></tr>");

            //loop through all the album's sections
            foreach (var section in album.AlbumSections)
            {
                //insert table row for this section's editor
                sb.Append("<tr>");
                sb.Append("<td>" + section.Title + "</td>");
                var editorName = "";
                if (!string.IsNullOrEmpty(section.Editor.FirstName) && !string.IsNullOrEmpty(section.Editor.LastName))
                {
                    editorName = section.Editor.FirstName + " " + section.Editor.LastName;
                }
                sb.Append("<td>" + editorName + "</td>");
                sb.Append("<td>" + section.Editor.Password + "</td>");
                sb.Append("</tr>");
            }

            sb.Append("</table>");

            return sb.ToString();
        }
    }
}